package com.xidian.zhiancloud.advice;

import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Summerless
 * @Date: 2020/9/28 17:08
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(CustomizeException.class)
    public ResultDTO CustomizeExceptionHandler(CustomizeException exception) {
        log.info("出现了自定义异常，异常原因为{}", exception.getMessage());
        return ResultDTO.errorOf(exception.getResponseEnums());
    }

    @ExceptionHandler(Exception.class)
    public ResultDTO globalExceptionHandler(HttpServletRequest request, Exception ex) {
        log.info("出现了无法处理的异常，异常原因为{}", ex.getMessage());
        return ResultDTO.errorOf(GeneralResponseEnums.SYSTEM_ERROR);
    }
}
